package Server;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;

import General.AdminServiceInterface;
import General.ShipQuoterServiceInterface;
import General.ShipmentIteratorInterface;


public class AdminService extends UnicastRemoteObject implements AdminServiceInterface {
	
	private static final long serialVersionUID = 1L;
	private SubServerService fSubServerService;
	
	public AdminService(SubServerService subServers) throws RemoteException {
		super(0, new SslRMIClientSocketFactory(), new SslRMIServerSocketFactory(null, null, true));
		fSubServerService = subServers;
	}

	public void changeRate(int shippingType, int rate) throws RemoteException {
		fSubServerService.changeRate(shippingType, rate);

	}

	public ShipmentIteratorInterface getPendingShipments() throws RemoteException {
		ShipmentIterator it = new ShipmentIterator(fSubServerService, true);
		return it;
	}
	
	public ShipmentIteratorInterface getHistoryShipments() throws RemoteException {
		ShipmentIterator it = new ShipmentIterator(fSubServerService, false);
		return it;
	}

	public void serializeSubServer(int serverNr, ObjectOutputStream outputFile) throws RemoteException {
		ShipQuoterServiceInterface subServer = fSubServerService.getServer(serverNr);
		try {
			outputFile.writeObject(subServer);
			System.out.println("Subserver serialized");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
}
